package cn.lyd.product.service.impl;

import cn.lyd.base.enm.ProductStateEnum;
import cn.lyd.base.jwt.UserInfo;
import cn.lyd.base.until.BaiduAuditUtils;
import cn.lyd.base.until.BussinessException;
import cn.lyd.base.until.ThreadLocalUtil;
import cn.lyd.org.domain.Shop;
import cn.lyd.pet.domain.Pet;
import cn.lyd.pet.domain.PetOnlineAuditLog;
import cn.lyd.product.domain.Product;
import cn.lyd.product.domain.ProductDetail;
import cn.lyd.product.domain.ProductOnlineAuditLog;
import cn.lyd.product.mapper.ProductDetailMapper;
import cn.lyd.product.mapper.ProductMapper;
import cn.lyd.product.mapper.ProductOnlineAuditLogMapper;
import cn.lyd.product.service.IProductService;
import cn.lyd.base.service.impl.BaseServiceImpl;
import cn.lyd.user.domain.Logininfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyd
 * @since 2023-01-13
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper detailMapper;
    @Autowired
    private ProductOnlineAuditLogMapper logMapper;


    /**
     * 重写添加服务--和明细
     * @param product
     */
    @Override
    public void add(Product product) {
        //参数校验

        UserInfo userInfo = ThreadLocalUtil.getUserInfo();
        Logininfo logininfo = userInfo.getLogininfo();
        Shop shop = logininfo.getShop();

        //生成服务
        Product addPro = new Product();
        addPro.setName(product.getName());
        addPro.setCostprice(product.getCostprice());
        addPro.setSaleprice(product.getSaleprice());
        addPro.setSalecount(product.getSalecount());
        addPro.setState(ProductStateEnum.ACTIVITY_NO_PASS.getCode());
        addPro.setResources(product.getResources());
        addPro.setCreatetime(new Date());
        addPro.setDetail(product.getDetail());

        //生成服务明细
        ProductDetail detail = new ProductDetail();

        ProductDetail addProDetail = addPro.getDetail();
        if (addProDetail!=null){
            detail.setIntro(addProDetail.getIntro());
            detail.setOrderNotice(addProDetail.getOrderNotice());
        }

        //有shopid才能添加
        if(shop!=null){
            //添加服务
            addPro.setShop_id(shop.getId());
            productMapper.add(addPro);

            productMapper.updateByShopId(shop.getId(),addPro.getId());

            //设置服务id到细节里
            detail.setProduct_id(addPro.getId());
            //添加明细
            detailMapper.add(detail);
        }else {
            throw new BussinessException("非店铺人员无权限添加！");
        }
    }


    /**
     * 重写删除-明细
     * @param id 服务id
     */
    @Override
    public void delete(Long id) {
        //删除明细
        detailMapper.delByProId(id);

        //删除服务
        super.delete(id);
    }


    /**
     * 通过服务id找明细
     * @param id
     * @return
     */
    @Override
    public Product findDetailById(Long id) {
        return productMapper.findByProId(id);
    }


    /**
     * 修改重写-明细
     * @param product
     */
    @Override
    public void update(Product product) {
        //先修改服务
        Product updateProduct = new Product();
        BeanUtils.copyProperties(product,updateProduct);
        //修改了如果是上架状态要变成下架，重新上架
        if (product.getState().equals(ProductStateEnum.ACTIVITY.getCode())){
            updateProduct.setState(ProductStateEnum.ACTIVITY_NO_PASS.getCode());
            updateProduct.setOffsaletime(new Date());
        }
        super.update(updateProduct);

        //如果原数据有明细
        if (updateProduct.getDetail().getId()!=null){
            ProductDetail newDetail = new ProductDetail();
            newDetail.setId(updateProduct.getDetail().getId());
            newDetail.setProduct_id(updateProduct.getId());
            newDetail.setIntro(updateProduct.getDetail().getIntro());
            newDetail.setOrderNotice(updateProduct.getDetail().getOrderNotice());
            //再修改明细
            detailMapper.update(newDetail);
        }
    }
    
    
    /**
     * 服务上架
     * @param ids
     */
    @Override
    public void onsale(List<Long> ids) {
        //遍历前端传来的服务id数组-请求上架
        for (Long id : ids){
            //1.获取每一个服务
            Product product = productMapper.findById(id);

            //2.百度ai判断是否违规
            StringBuffer sb = new StringBuffer();
            if(!BaiduAuditUtils.TextCensor(product.getName())){
                sb.append("服务名称审核失败,服务名称不合法："+product.getName());
            }
//            for (String filePath:pet.getResources().split(",")) {
//                if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+filePath)){
//                    sb.append("服务图片审核失败,服务图片不合法:"+"http://123.207.27.208"+filePath);
//                }
//            }

            //3.存在违规就执行---添加审核失败的日志
            if (sb.length()>0){
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProduct_id(id);
                log.setState(0);//状态 0失败 1成功
                log.setAudit_id(350L);//admin
                log.setAudit_time(new Date());
                log.setNote("审核未通过！存在非法信息！");
                logMapper.add(log);
                //继续遍历
                continue;
            }
            //不违规，上架
            productMapper.onsale(id);
            ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            log.setProduct_id(id);
            log.setState(1);//状态 0失败 1成功
            log.setAudit_id(350L);//admin
            log.setAudit_time(new Date());
            log.setNote("审核通过!上架成功！");
            logMapper.add(log);
        }
    }


    /**
     * 服务下架
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        //设置状态为0-下架，添加下架时间
        productMapper.offsale(ids);

        //添加下架日志
        ProductOnlineAuditLog log = new ProductOnlineAuditLog();
        //遍历ids下架的服务的id，设置日志的服务id为遍历数组中的id
        ids.stream().forEach(id-> log.setProduct_id(id));
        log.setState(1);//状态 0失败 1成功
        log.setAudit_id(350L);//admin
        log.setAudit_time(new Date());
        log.setNote("下架成功！");
        logMapper.add(log);

    }

    /**
     * 前台展示服务详情
     * @param id
     * @return
     */
    @Override
    public Product findShowById(Long id) {
        return productMapper.findShowById(id);
    }


}
